<template>
  <div class="page-container">
    <div class="page-search-card">
      <el-form ref="form" label-width="150px" :model="form">
        <el-row>
          <el-col :span="6">
            <el-form-item label="岗位名称：" prop="positionName">
              <el-input v-model="form.positionName" placeholder="请输入"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="18">
            <el-form-item label-width="25px">
              <el-button @click="reset">重置</el-button>
              <el-button type="primary" @click="query">查询</el-button>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </div>
    <div class="page-operation-row">
      <el-button @click="onAdd">新增岗位</el-button>
    </div>
    <div class="page-table-container" style="height: calc(100% - 146px)">
      <wf-el-table
        height="100%"
        :list="list"
        :total="total"
        :loading="loading"
        :current="current"
        @on-page-change="onPageChange"
      >
        <el-table-column label="序号" type="index" width="60"></el-table-column>
        <el-table-column prop="positionName" label="岗位名称"></el-table-column>
        <el-table-column prop="positionType" label="类型">
          <template slot-scope="scope">
            {{ ['普通', '管理'][scope.row.positionType] || '' }}
          </template>
        </el-table-column>
        <el-table-column prop="jobScope" label="职责范围"></el-table-column>
        <el-table-column prop="positionNumber" label="人数"></el-table-column>
        <el-table-column label="操作" fixed="right" width="350">
          <template slot-scope="scope">
            <el-button type="text" size="small" @click="onDetail(scope.row)">查看</el-button>
            <el-button type="text" size="small" @click="onEdit(scope.row)">编辑</el-button>
            <el-button
              v-if="scope.row.positionNumber === 0"
              type="text"
              size="small"
              @click="onDelete(scope.row)"
            >
              删除
            </el-button>
          </template>
        </el-table-column>
      </wf-el-table>
    </div>
    <add-position ref="addPosition" @saved="query" />
    <detail-position ref="detailPositon" />
  </div>
</template>

<script>
  import { mapGetters } from 'vuex';
  import AddPosition from './drawer/AddPosition';
  import DetailPosition from './drawer/DetailPosition';
  import { getPositionList, deletePosition } from '@/api/position';

  export default {
    components: { AddPosition, DetailPosition },
    data() {
      return {
        form: {
          positionName: null,
        },
        list: [],
        total: 0,
        current: 1,
        loading: false,
      };
    },
    computed: {
      ...mapGetters('user', ['userInfo']),
      organizationId() {
        return this.userInfo.organId;
      },
    },
    created() {
      this.query();
    },
    methods: {
      async initTable() {
        const params = {
          current: this.current,
          size: 10,
          organizationId: this.organizationId,
          ...this.form,
        };
        this.loading = true;
        const { data = {} } = await getPositionList(params);
        const { records = [], total = 0 } = data;
        this.total = total;
        this.list = records;
        this.loading = false;
      },
      query() {
        this.current = 1;
        this.initTable();
      },
      reset() {
        this.$refs['form'].resetFields();
      },
      onAdd() {
        this.$refs['addPosition'].open(this.organizationId);
      },
      onDetail(row) {
        this.$refs['detailPositon'].open(row);
      },
      onEdit(row) {
        this.$refs['addPosition'].edit(row);
      },
      onDelete(row) {
        this.$confirm('您是否确定删除此岗位名称？', '提示', {
          confirmButtonText: '确定并删除',
          cancelButtonText: '取消',
          type: 'warning',
        }).then(async () => {
          await deletePosition(row.id);
          this.query();
        });
      },
      onPageChange(data) {
        this.current = data.current;
        this.initTable();
      },
    },
  };
</script>

<style lang="scss" scoped>
  .page-operation-row {
    margin-bottom: 15px;
  }
</style>
